/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models.view;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import models.beans.Usuariobeans;
import models.dao.Usuariodao;

/**
 *
 * @author cepardov
 */
public class MantenedorUsuario extends javax.swing.JFrame {
    String rut,nombre,paterno,materno,privilegio,clave;
    String rutin;
    boolean estadoFom;
    Usuariobeans ub=new Usuariobeans();
    Usuariodao ud=new Usuariodao();
    Object [][] dttipo;
    int fila, idtipo=0;
    /**
     * Creates new form MantenedorUsuario
     * @param rut
     * @param nombre
     * @param paterno
     * @param materno
     * @param privilegio
     * @param clave
     * @param primerInicio
     */
    public MantenedorUsuario(String rut, String nombre, String paterno, String materno, String privilegio, String clave, boolean primerInicio) {
        initComponents();
        this.updateTabla();
        this.primerInicio(primerInicio);
        this.rutin=this.txtrut.getText();
        this.btnmodify.setEnabled(false);
        this.btndelete.setEnabled(false);
        
        //set variables locales con datos sensibles
        this.rut=rut;
        this.nombre=nombre;
        this.paterno=paterno;
        this.materno=materno;
        this.privilegio=privilegio;
        this.clave=clave;
        System.out.println("estado de llegada="+primerInicio);
    }
    
    public MantenedorUsuario(){
    }
    
    private void mostrarLogin(){
        LoginServ login=new LoginServ();
        login.setTitle("Inicio de sesión");
        login.setVisible(true);
        dispose();
        JOptionPane.showMessageDialog(null,"El usuario administrador "+ub.getNombre()+" "+ub.getPaterno()+" se ha creado con exito","Configuraciones iniciales", JOptionPane.INFORMATION_MESSAGE);
    }
    
    private void validarVacio(String campo){
        JOptionPane.showMessageDialog(null,"El campo "+campo+" es requrido. Ingrese los datos solicitados.","Validación de formularios", JOptionPane.ERROR_MESSAGE);
    }
    
    private void primerInicio(boolean estadoPI){
        if(estadoPI==true){
            this.estadoFom=estadoPI;
            System.out.println("EstadoForm="+this.estadoFom);
            cbprivilegio.setSelectedItem("Administrador");
            cbprivilegio.setEnabled(false);
            btnbuscarrut.setEnabled(false);
            jpTabla.setVisible(false);
            btnupdate.setEnabled(false);
            this.btnVolverMenu.setEnabled(false);
            this.setSize(650, 360);
        } else {
            this.btnCerrar.setVisible(false);
        }
    }
    
    private void updateTabla(){
        String[] columNames = {"RUT","Nombre","Paterno","Materno","Teléfono","E-Mail","Privilegio","Clave"};  
        dttipo = ud.getUsuario();
        DefaultTableModel datos = new DefaultTableModel(dttipo,columNames);                        
        tabla.setModel(datos);
    }
    
    private void clear(){
        this.txtrut.setText("");
        this.txtnombre.setText("");
        this.txtpaterno.setText("");
        this.txtmateno.setText("");
        this.txttelefono.setText("");
        this.txtemail.setText("");
        this.cbprivilegio.setSelectedItem("Seleccione");
        this.txtpass.setText("");
        this.txtpassre.setText("");
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpUsuarios = new javax.swing.JPanel();
        btnsave = new javax.swing.JButton();
        btnmodify = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        btnupdate = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtrut = new javax.swing.JFormattedTextField();
        btnbuscarrut = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtnombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtpaterno = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtmateno = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txttelefono = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        txtemail = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        cbprivilegio = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        txtpass = new javax.swing.JPasswordField();
        jLabel9 = new javax.swing.JLabel();
        txtpassre = new javax.swing.JPasswordField();
        btnlimpiar = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        btnVolverMenu = new javax.swing.JButton();
        jpTabla = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jpUsuarios.setBorder(javax.swing.BorderFactory.createTitledBorder("Usuarios"));

        btnsave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/utilidades/IMG/guardar.png"))); // NOI18N
        btnsave.setText("Guardar");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

        btnmodify.setText("Modificar");
        btnmodify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnmodifyActionPerformed(evt);
            }
        });

        btndelete.setText("Eliminar");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(evt);
            }
        });

        btnupdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/utilidades/IMG/Actualizar.png"))); // NOI18N
        btnupdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnupdateActionPerformed(evt);
            }
        });

        jLabel1.setText("RUT");

        try {
            txtrut.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###-A")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        btnbuscarrut.setText("Buscar");

        jLabel2.setText("Nombre");

        jLabel3.setText("Apellido Paterno");

        jLabel4.setText("Apellido Materno");

        jLabel5.setText("Teléfono");

        jLabel6.setText("Email");

        jLabel7.setText("Privilegio");

        cbprivilegio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione", "Administrador", "Recepcionista" }));

        jLabel8.setText("Contraseña");

        jLabel9.setText("Reingrese Contraseña");

        btnlimpiar.setText("Limpiar");
        btnlimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlimpiarActionPerformed(evt);
            }
        });

        btnCerrar.setText("cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        btnVolverMenu.setText("Volver al menú");
        btnVolverMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVolverMenuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpUsuariosLayout = new javax.swing.GroupLayout(jpUsuarios);
        jpUsuarios.setLayout(jpUsuariosLayout);
        jpUsuariosLayout.setHorizontalGroup(
            jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpUsuariosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpUsuariosLayout.createSequentialGroup()
                        .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jpUsuariosLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtrut, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnbuscarrut))
                            .addGroup(jpUsuariosLayout.createSequentialGroup()
                                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txttelefono, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                                        .addComponent(txtnombre, javax.swing.GroupLayout.Alignment.LEADING))
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jpUsuariosLayout.createSequentialGroup()
                                        .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtpaterno, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel3))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel4)
                                            .addComponent(txtmateno, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpUsuariosLayout.createSequentialGroup()
                        .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jpUsuariosLayout.createSequentialGroup()
                                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jpUsuariosLayout.createSequentialGroup()
                                        .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel5)
                                            .addComponent(jLabel7))
                                        .addGap(112, 112, 112)
                                        .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jpUsuariosLayout.createSequentialGroup()
                                                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(jLabel8))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(txtpassre, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(jLabel9)))
                                            .addComponent(jLabel6)))
                                    .addComponent(cbprivilegio, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnCerrar))
                            .addGroup(jpUsuariosLayout.createSequentialGroup()
                                .addComponent(btnsave)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnlimpiar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnmodify)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btndelete)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnupdate)
                                .addGap(18, 18, 18)
                                .addComponent(btnVolverMenu)))
                        .addGap(37, 37, 37))))
        );
        jpUsuariosLayout.setVerticalGroup(
            jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpUsuariosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtrut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnbuscarrut))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtpaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtmateno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txttelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbprivilegio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtpassre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCerrar))
                .addGap(18, 21, Short.MAX_VALUE)
                .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnupdate, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jpUsuariosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnsave)
                            .addComponent(btnmodify)
                            .addComponent(btndelete)
                            .addComponent(btnlimpiar)))
                    .addComponent(btnVolverMenu))
                .addContainerGap())
        );

        jpTabla.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Usuarios"));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);

        javax.swing.GroupLayout jpTablaLayout = new javax.swing.GroupLayout(jpTabla);
        jpTabla.setLayout(jpTablaLayout);
        jpTablaLayout.setHorizontalGroup(
            jpTablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE)
        );
        jpTablaLayout.setVerticalGroup(
            jpTablaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpUsuarios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpTabla, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpUsuarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jpTabla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        // TODO add your handling code here:
        this.setAlwaysOnTop(false);
        String rutusuario = this.txtrut.getText();
        String nombreU = this.txtnombre.getText();
        String paternoU = this.txtpaterno.getText();
        String maternoU = this.txtmateno.getText();
        String telefono = this.txttelefono.getText();
        String mail = this.txtemail.getText();
        String privilegioU = this.cbprivilegio.getSelectedItem().toString();
        String claveU = this.txtpass.getText();
        String clave2 = this.txtpassre.getText();

        //validaciones
        int lNombre = this.txtnombre.getText().length();
        int maxNombre=20;
        if(lNombre>maxNombre){
            JOptionPane.showMessageDialog(null,"El nombre excede el maximo permitido en la base de datos\n"
                    + "\t\t\""+nombreU+"\" debe ser menor a "+maxNombre+" caracteres.","Validación de formularios", JOptionPane.ERROR_MESSAGE);
        }
        if(rutin.equals(rutusuario)){
            System.out.println("in="+rutin);
            System.out.println("out="+rutusuario);
            this.validarVacio("RUT Usuario");
        } else if(this.txtnombre.getText().isEmpty()){
            this.validarVacio("nombre");
        } else if(this.txtpaterno.getText().isEmpty()){
            this.validarVacio("apellido paterno");
        }else if(this.txtmateno.getText().isEmpty()){
            this.validarVacio("apellido materno");
        }else {
            ub.setRutusuario(rutusuario);
            ub.setNombre(nombreU);
            ub.setPaterno(paternoU);
            ub.setMaterno(maternoU);
            ub.setTelefono(telefono);
            ub.setMail(mail);
            ub.setPrivilegio(privilegioU);
            ub.setClave(claveU);
            ub.save();
            if(this.estadoFom==true){
                this.mostrarLogin();
            } else {
                this.updateTabla();
                this.btnsave.setVisible(false);
                this.btnmodify.setEnabled(true);
                this.btndelete.setEnabled(true);
            }
        }
    }//GEN-LAST:event_btnsaveActionPerformed

    private void btnmodifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnmodifyActionPerformed
        // TODO add your handling code here:
        String rutusuario = this.txtrut.getText();
        String nombreU = this.txtnombre.getText();
        String paternoU = this.txtpaterno.getText();
        String maternoU = this.txtmateno.getText();
        String telefono = this.txttelefono.getText();
        String mail = this.txtemail.getText();
        String privilegioU = this.cbprivilegio.getSelectedItem().toString();
        String claveU = this.txtpass.getText();
        String clave2 = this.txtpassre.getText();
        
        //Validación formulario
        
        ub.setRutusuario(rutusuario);
        ub.setNombre(nombreU);
        ub.setPaterno(paternoU);
        ub.setMaterno(maternoU);
        ub.setTelefono(telefono);
        ub.setMail(mail);
        ub.setPrivilegio(privilegioU);
        ub.setClave(claveU);
        
        ub.update();
        this.updateTabla();
    }//GEN-LAST:event_btnmodifyActionPerformed

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        // TODO add your handling code here:
        fila = tabla.rowAtPoint(evt.getPoint());
        if (fila > -1){
            this.txtrut.setText(String.valueOf(tabla.getValueAt(fila, 0)));
            this.txtnombre.setText(String.valueOf(tabla.getValueAt(fila, 1)));
            this.txtpaterno.setText(String.valueOf(tabla.getValueAt(fila, 2)));
            this.txtmateno.setText(String.valueOf(tabla.getValueAt(fila, 3)));
            this.txttelefono.setText(String.valueOf(tabla.getValueAt(fila, 4)));
            this.txtemail.setText(String.valueOf(tabla.getValueAt(fila, 5)));
            this.cbprivilegio.setSelectedItem(String.valueOf(tabla.getValueAt(fila, 6)));
            this.txtpass.setText(String.valueOf(tabla.getValueAt(fila, 7)));
        }
        this.btnmodify.setEnabled(true);
        this.btndelete.setEnabled(true);
        this.btnsave.setVisible(false);
    }//GEN-LAST:event_tablaMouseClicked

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndeleteActionPerformed
        // TODO add your handling code here:
        ub.setRutusuario(this.txtrut.getText());
        ub.delete();
        this.clear();
        this.updateTabla();
        this.btnmodify.setEnabled(false);
        this.btndelete.setEnabled(false);
        this.btnsave.setVisible(true);
    }//GEN-LAST:event_btndeleteActionPerformed

    private void btnupdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnupdateActionPerformed
        // TODO add your handling code here:
        this.updateTabla();
    }//GEN-LAST:event_btnupdateActionPerformed

    private void btnlimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlimpiarActionPerformed
        // TODO add your handling code here:
        this.updateTabla();
        this.clear();
        this.btnsave.setVisible(true);
        this.btnmodify.setEnabled(false);
        this.btndelete.setEnabled(false);
    }//GEN-LAST:event_btnlimpiarActionPerformed

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnVolverMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVolverMenuActionPerformed
        // TODO add your handling code here:
        Menu men=new Menu(rut,nombre,paterno,materno,privilegio,clave);
        men.setLocationRelativeTo(null);
        men.setTitle("Gestión Hotelera - "+nombre+" "+paterno+" "+materno+" ["+privilegio+"]");
        men.setVisible(true);
        dispose();
    }//GEN-LAST:event_btnVolverMenuActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MantenedorUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MantenedorUsuario().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnVolverMenu;
    public javax.swing.JButton btnbuscarrut;
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnlimpiar;
    private javax.swing.JButton btnmodify;
    private javax.swing.JButton btnsave;
    public javax.swing.JButton btnupdate;
    public javax.swing.JComboBox cbprivilegio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JPanel jpTabla;
    private javax.swing.JPanel jpUsuarios;
    public javax.swing.JTable tabla;
    private javax.swing.JTextField txtemail;
    private javax.swing.JTextField txtmateno;
    private javax.swing.JTextField txtnombre;
    private javax.swing.JPasswordField txtpass;
    private javax.swing.JPasswordField txtpassre;
    private javax.swing.JTextField txtpaterno;
    private javax.swing.JFormattedTextField txtrut;
    private javax.swing.JFormattedTextField txttelefono;
    // End of variables declaration//GEN-END:variables
}

